program C;

{$APPTYPE CONSOLE}

{$R *.res}

uses
  System.SysUtils;

type
  TAttack = record
    d: Integer;
    s: Integer;
    w: Integer;
    e: Integer;
  end;
  TAttackArray = Array of TAttack;
var
  f, g: TextFile;
  answer: Int64;
  T: Word;
  N, na, ca: Integer;
  attacks: TAttackArray;

 procedure QuickSort(var A: TAttackArray; iLo, iHi: Integer) ;
 var
   Lo, Hi, Pivot: Integer;
   T: TAttack;
 begin
   Lo := iLo;
   Hi := iHi;
   Pivot := A[(Lo + Hi) div 2].d;
   repeat
     while A[Lo].d < Pivot do Inc(Lo) ;
     while A[Hi].d > Pivot do Dec(Hi) ;
     if Lo <= Hi then
     begin
       T := A[Lo];
       A[Lo] := A[Hi];
       A[Hi] := T;
       Inc(Lo) ;
       Dec(Hi) ;
     end;
   until Lo > Hi;
   if Hi > iLo then QuickSort(A, iLo, Hi) ;
   if Lo < iHi then QuickSort(A, Lo, iHi) ;
 end;

procedure ReadData;
var
  i, j, d, ni, w, e, s, ddi, dsi, dpi: Integer;
begin
  Readln(f, N);
  na := 0;
  for i := 1 to N do begin
    Readln(f, d, ni, w, e, s, ddi, dsi, dpi);
    ca := na;
    Inc(na, ni);
    SetLength(attacks, na);
    for j := 0 to ni - 1 do begin
      attacks[ca + j].d := d + j * ddi;
      attacks[ca + j].s := s + j * dsi;
      attacks[ca + j].w := w + j * dpi;
      attacks[ca + j].e := e + j * dpi;
    end;
  end;
  QuickSort(attacks, 0, na - 1);
end;

procedure ProcessData;
type
  TPend = record
    s: Integer;
    w: Integer;
    e: Integer;
  end;
  TPendArray = Array of TPend;

var
  i, j, curday, pendingCount: Integer;
  wall: Array of Int64;
  pa: TPendArray;
  k: Integer;
begin
  curday := 0;
  answer := 0;
  pendingCount := 0;
  //for i := -101000000 to 101000000 do
  //  wall[i] := 0;

  for i := 0 to na - 1 do begin
    if attacks[i].d > curday then begin
      if pendingCount > 0 then begin
        for j := 0 to pendingCount - 1 do
          for k := pa[j].w to pa[j].e do
            if (wall[k] < pa[j].s) then
              wall[k] := pa[j].s;
      end;
    end;
    curday := attacks[i].d;
    pendingCount := 0;
    SetLength(pa, 0);
    for k := attacks[i].w to attacks[i].e do
      if (wall[k] < attacks[i].s) then begin
        Inc(answer);
        Inc(pendingCount);
        pa[pendingCount - 1].s := attacks[i].s;
        pa[pendingCount - 1].w := attacks[i].w;
        pa[pendingCount - 1].w := attacks[i].e;
        Break;
      end;
  end;
end;


procedure OutputVerdict(casenum: Word);
begin
  writeln(g, 'Case #', casenum, ': ', answer);
end;

var
  i: Word;
  s: String;

begin
  try
    s := ParamStr(1);
    if (s = '') then
       s := 'input.txt';
    AssignFile(f, s);
    Reset(f);
    AssignFile(g, 'output.txt');
    Rewrite(g);
    try
      Readln(f, T);
      for i := 1 to T do begin
        ReadData;
        ProcessData;
        OutputVerdict(i);
      end;
    finally
      CloseFile(f);
      CloseFile(g);
    end;
    writeln('Reading finished');
    Readln;
  except
    on E: Exception do begin
      Writeln(E.ClassName, ': ', E.Message);
      readln;
    end;
  end;
end.
